package com.windystory.wslibrary.ui.textview;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.TextUtils;
import android.util.AttributeSet;

import com.windystory.wslibrary.R;
import com.windystory.wslibrary.util.LogUtil;
import com.windystory.wslibrary.util.StringExUtil;

/**
 * TextView控件
 */
public class UILabel extends androidx.appcompat.widget.AppCompatTextView {

    public static final int NORMAL = 0;
    public static final int PASSWORD = 1;
    public static final int PHONE = 2;

    public UILabel(Context context) {
        super(context);
    }

    public UILabel(Context context, AttributeSet attrs) {
        super(context, attrs);
        getAttrs(context, attrs);
    }

    public UILabel(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        getAttrs(context, attrs);
    }

    private void getAttrs(Context context, AttributeSet attrs) {
        if (attrs != null) {
            TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.mTextView);
            int TEXT_STYLE = a.getInt(R.styleable.mTextView_styleTextView, NORMAL);
            drawStyle(TEXT_STYLE);
        }
    }

    public void drawStyle(int TEXT_STYLE) {
        switch (TEXT_STYLE) {
            case NORMAL:
                break;
            case PASSWORD://密码类型
                if (!TextUtils.isEmpty(getText().toString())) {
                    long len = getText().toString().length();
                    StringBuffer buffer = new StringBuffer();
                    for (int i = 0; i < len; i++) {
                        buffer.append("*");
                    }
                    setText(buffer.toString());
                }
                break;
            case PHONE:
                if (!TextUtils.isEmpty(getText().toString()) && StringExUtil.isMobile(getText().toString())) {
                    char[] len = getText().toString().toCharArray();
                    if (len.length == 11) {
                        StringBuffer buffer = new StringBuffer();
                        for (int i = 0; i < len.length; i++) {
                            if (i > 2 && i < 7)
                                buffer.append("*");
                            else
                                buffer.append(len[i]);
                        }
                        setText(buffer.toString());
                    }
                }
                break;
        }

    }
}
